package cn.buk.tms.eotms.controller;

import cn.buk.common.JsonResult;
import cn.buk.tms.exception.BaseException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import jakarta.servlet.http.HttpServletRequest;

/**
 * @author yfdai
 */
@RestControllerAdvice
public class GlobalExceptionHandler extends BaseController {
  private static final Logger logger = LogManager.getLogger(GlobalExceptionHandler.class);

  /**
   * 接口参数校验异常
   */
  @ExceptionHandler(value = {Exception.class})
  public JsonResult handleGlobalException(HttpServletRequest request, Exception ex) {
    logger.error(ex.getMessage());
    ex.printStackTrace();

    return JsonResult.createJsonResult(-10000, ex.getMessage());
  }

  @ExceptionHandler(value = {BaseException.class})
  public JsonResult handleTmsBaseException(HttpServletRequest request, BaseException ex) {
    logger.warn("{}, {}", ex.getErrcode(), ex.getMessage());

    return JsonResult.createJsonResult(-ex.getErrcode(), ex.getMessage());
  }


}

